package com.xrtn.controller;

import com.xrtn.bean.SysUserInfo;
import com.xrtn.bean.TBookingInfo;
import com.xrtn.bean.TOrderInfo;
import com.xrtn.page.Page;
import com.xrtn.service.ICommonService;
import com.xrtn.service.IDoctorService;
import com.xrtn.service.ISysOrderService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.HttpUtils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tu_pei on 2018/3/5.
 */
@Api("订单相关接口")
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private ICommonService commonService;

    /**
     * 进入订单管理页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoOrder", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoOrder(ModelAndView modelAndView) {
        modelAndView.setViewName("orderList/orderManagement");
        return modelAndView;
    }

    /**
     * 进入快速购买商品页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoHomeGoods", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoHomeGoods(ModelAndView modelAndView) {
        modelAndView.setViewName("home/home-goods");
        return modelAndView;
    }

    /**
     * 进入订单详情页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoOrderInfo", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoOrderInfo(ModelAndView modelAndView) {
        modelAndView.setViewName("orderList/orderListContent");
        return modelAndView;
    }

    /**
     * 进入快速开单页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoFastOrder", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoFastOrder(ModelAndView modelAndView, Integer bookingId) {
        modelAndView.setViewName("home/home-billing");

        TBookingInfo bookingInfo = commonService.selectBookingByPrimaryKey(bookingId);

        if (ObjUtils.isNotEmpty(bookingInfo) && bookingInfo.getBookingStatus() == 0) {
            bookingInfo.setBookingStatus(6);
            commonService.updateBookingInfo(bookingInfo);
        }

        return modelAndView;
    }

    /**
     * 进入订单详情
     *
     * @return
     */
    @RequestMapping(value = "/gotoOrderDetail", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoOrderDetail(ModelAndView modelAndView) {
        modelAndView.setViewName("orderList/orderManagementdetail");
        return modelAndView;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: saveOrderInfo
     * @Description: 保存订单
     */
    @ApiOperation(value = "保存订单-开始调理", notes = "保存订单-开始调理")
    @RequestMapping(value = "/saveOrderInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "bookingId", dataType = "int", required = true, value = "预约id", defaultValue = "181"),
            @ApiImplicitParam(paramType = "query", name = "doctorId", dataType = "string", required = true, value = "医师id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "分店id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "patientId", dataType = "int", required = true, value = "患者id", defaultValue = "279"),
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "家长id", defaultValue = "56"),
            @ApiImplicitParam(paramType = "query", name = "cashierId", dataType = "int", required = true, value = "后台登录用户", defaultValue = "7"),
            @ApiImplicitParam(paramType = "query", name = "userName", dataType = "string", required = true, value = "用户信息", defaultValue = "兔兔"),
            @ApiImplicitParam(paramType = "query", name = "cardId", dataType = "string", required = false, value = "次卡id", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "couponId", dataType = "int", required = false, value = "优惠券id", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "orderTotalCost", dataType = "double", required = true, value = "应付金额", defaultValue = "277"),
            @ApiImplicitParam(paramType = "query", name = "orderPayCost", dataType = "double", required = true, value = "实付金额", defaultValue = "277"),
            @ApiImplicitParam(paramType = "query", name = "goodsList", dataType = "string", required = true, value = "药品数组", defaultValue = "[{\"goodsId\":2,\"goodsName\":\"咳嗽-药膏\",\"goodsImg\":\"123\",\"goodsCost\":79,\"goodsIncomeRatio\":0.1,\"orderNum\":2}]"),
            @ApiImplicitParam(paramType = "query", name = "serviceList", dataType = "string", required = true, value = "推拿服务数组", defaultValue = "[{\"serviceId\":1,\"serviceName\":\"一般推拿\",\"serviceImg\":\"0.jpg\",\"serviceCost\":198,\"serviceIncome\":0.22}]"),
    })
    @ResponseBody
    public ResponseData saveOrderInfo(HttpServletRequest request, TOrderInfo orderInfo, String goodsList, String serviceList) {
        SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);
        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        if (ObjUtils.isEmpty(orderInfo.getCashierId())) {
            orderInfo.setCashierId(userInfo.getUserId());
        }
        if (ObjUtils.isEmpty(orderInfo.getSubclinicId())) {
            orderInfo.setSubclinicId(userInfo.getSubclinicId());
        }
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderInfo.getBookingId(),
                orderInfo.getUserId(), orderInfo.getDoctorId(), orderInfo.getPatientId(),
                orderInfo.getOrderTotalCost(), orderInfo.getOrderPayCost(), orderInfo.getCouponTotalCost()
        });
        orderInfo.setCreateTime(new Date());
        orderInfo.setOrderState(3);
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }
        Integer count = doctorService.saveOrderInfo(orderInfo, goodsList, serviceList);
        if (count > 0) {
            data.setResults(count);
            data.setCodeEnum(CodeEnum.SUCCESS);
            return data;
        }
        if (count == 0) {
            data.setCodeEnum(CodeEnum.CODE_30002);
            return data;
        } else if (count == -1) {
            data.setCodeEnum(CodeEnum.CODE_30005);
        } else if (count == -2) {
            data.setCodeEnum(CodeEnum.CODE_30004);
        } else if (count == -3) {
            data.setCodeEnum(CodeEnum.CODE_30006);
        }

        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: updateOrderInfo
     * @Description: 修改订单明细
     */
    @ApiOperation(value = "修改订单明细", notes = "修改订单明细")
    @RequestMapping(value = "/updateOrderInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", required = true, value = "订单id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "bookingId", dataType = "int", required = false, value = "预约id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "doctorId", dataType = "string", required = false, value = "医师id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = false, value = "分店id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "patientId", dataType = "int", required = false, value = "患者id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "cardId", dataType = "string", required = false, value = "次卡id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "couponId", dataType = "int", required = false, value = "优惠券id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = false, value = "家长id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderTotalCost", dataType = "double", required = false, value = "应付金额", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderPayCost", dataType = "double", required = false, value = "实付金额", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "payUserId", dataType = "int", required = false, value = "代付人id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsList", dataType = "array", required = false, value = "药品数组", defaultValue = "[{\"goodsId\":1,\"goodsName\":\"12\",\"goodsImg\":\"12\",\"goodsCost\":12.12,\"goodsIncomeRatio\":1.2,\"orderNum\":2}]"),
            @ApiImplicitParam(paramType = "query", name = "serviceList", dataType = "array", required = false, value = "推拿服务数组", defaultValue = "[{\"serviceId\":1,\"serviceName\":\"12\",\"serviceImg\":\"12\",\"serviceCost\":12.12,\"serviceIncome\":12.12}]"),
    })
    @ResponseBody
    public ResponseData updateOrderInfo(TOrderInfo orderInfo, String goodsList, String serviceList) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderInfo.getBookingId(),
                orderInfo.getUserId(), orderInfo.getDoctorId(), orderInfo.getSubclinicId(), orderInfo.getPatientId(),
                orderInfo.getOrderTotalCost(), orderInfo.getOrderPayCost(), orderInfo.getCouponTotalCost()
        });
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        Integer result = doctorService.updateOrderDetail(orderInfo, goodsList, serviceList);
        if (result == -1) { // 已完成订单无法修改
            data.setCodeEnum(CodeEnum.CODE_30014);
            return data;
        }
        data.setResults(orderInfo.getOrderId());
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectOrderInfo
     * @Description: 查询订单
     */
    @ApiOperation(value = "查询订单", notes = "查询订单")
    @RequestMapping(value = "/selectOrderInfo", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", required = true, value = "订单id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderType", dataType = "int", required = true, value = "0：订单管理，1：卡项订单", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectOrderInfo(Integer orderId, Integer orderType) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("orderType", orderType == null ? 0 : orderType);
        data.setResults(sysOrderService.selectOrderInfo(params));

        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 查询订单列表
     */
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @RequestMapping(value = "/selectOrderList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "patientId", dataType = "int", required = false, value = "患者id", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = false, value = "患者id", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "doctorId", dataType = "int", required = false, value = "大夫id", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = false, value = "手机号", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "startDate", dataType = "string", required = false, value = "开始时间", defaultValue = "2018-03-01"),
            @ApiImplicitParam(paramType = "query", name = "endDate", dataType = "string", required = false, value = "结束时间", defaultValue = "2018-03-05"),
            @ApiImplicitParam(paramType = "query", name = "payWay", dataType = "int", required = false, value = "付款方式:0:余额;1:支付宝二维码;2:微信二维码;3:现金;4:信用卡;5:次卡", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = false, value = "门店id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderState", dataType = "int", required = false, value = "状态：0完成，4取消，null为全部", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "dayType", dataType = "int", required = false, value = "0：全部，1：今天，2：近三天，3：近七天", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "isAdd", dataType = "int", required = false, value = "0继续治疗，1开启新病程", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "bookingType", dataType = "int", required = false, value = "0调理身体，1保健", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectOrderList(HttpServletRequest request, Integer patientId, Integer doctorId, Integer payWay, Integer dayType,
                                        String userPhone, Integer subclinicId, Integer orderState, String startDate, String endDate,
                                        Integer page, Integer limit, Integer isAdd, Integer bookingType, Integer userId) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        //Map<String,Object> params = HttpUtils.getAllReqParams(request);
        Map<String, Object> params = new HashMap<>();
        params.put("patientId", patientId);
        params.put("doctorId", doctorId);
        params.put("userId", userId);
        params.put("payWay", payWay);
        params.put("dayType", dayType);
        params.put("subclinicId", subclinicId);
        params.put("userPhone", userPhone);
        params.put("orderState", orderState);

        params.put("isAdd", isAdd);
        params.put("bookingType", bookingType);
        if (ObjUtils.isNotEmpty(startDate)) {
            params.put("startDate", startDate + " 00:00:00");
        }
        if (ObjUtils.isNotEmpty(endDate)) {
            params.put("endDate", endDate + " 23:59:59");
        }

        page = page == null ? 1 : page;
        limit = limit == null ? Integer.MAX_VALUE : limit;
        Page pages = Page.newBuilder(page, limit);

        Page patientPage = sysOrderService.selectOrderList(pages, params);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", patientPage.getItems());
        map.put("records", patientPage.getTotalRecord());
        map.put("total", patientPage.getTotalPage());
        map.put("page", patientPage.getPageNo());
        data.setResults(patientPage.getItems());

        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 查询订单列表
     */
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @RequestMapping(value = "/selectOrderPageList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "patientId", dataType = "int", required = false, value = "患者id", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "doctorId", dataType = "int", required = false, value = "大夫id", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = false, value = "手机号", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "startDate", dataType = "string", required = false, value = "开始时间", defaultValue = "2018-03-01"),
            @ApiImplicitParam(paramType = "query", name = "endDate", dataType = "string", required = false, value = "结束时间", defaultValue = "2018-03-05"),
            @ApiImplicitParam(paramType = "query", name = "payWay", dataType = "int", required = false, value = "付款方式:0:余额;1:支付宝二维码;2:微信二维码;3:现金;4:信用卡;5:次卡;6特殊：代表商品订单", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "payWays", dataType = "int", required = false, value = "付款方式:0:余额;1:支付宝二维码;2:微信二维码;3:现金;4:信用卡;5:次卡", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = false, value = "门店id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderState", dataType = "int", required = false, value = "状态：0完成，4取消，null为全部", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "treatmentState", dataType = "int", required = false, value = "0待完善，1已完成，2已失效", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "dayType", dataType = "int", required = false, value = "0：全部，1：今天，2：近三天，3：近七天", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "isAdd", dataType = "int", required = false, value = "0继续治疗，1开启新病程", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "bookingType", dataType = "int", required = false, value = "0调理身体，1保健", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "isTreatment", dataType = "int", required = false, value = "0调理身体，1保健", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectOrderPageList(HttpServletRequest request, Integer patientId, Integer doctorId, Integer payWay, Integer payWays, Integer dayType,
                                            String userPhone, Integer subclinicId, Integer orderState, Integer treatmentState, String startDate, String endDate,
                                            Integer page, Integer limit, Integer isAdd, Integer bookingType, Integer userId, Integer isTreatment) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        //Map<String,Object> params = HttpUtils.getAllReqParams(request);
        Map<String, Object> params = new HashMap<>();
        params.put("patientId", patientId);
        params.put("doctorId", doctorId);
        params.put("userId", userId);
        params.put("payWay", payWay);
        params.put("payWays", payWays);
        params.put("dayType", dayType);
        params.put("isTreatment", isTreatment);
        params.put("treatmentState", treatmentState);

        if (ObjUtils.isNotEmpty(subclinicId)) {
            if (subclinicId != 0) {
                params.put("subclinicId", subclinicId);
            }
        }
        if (ObjUtils.isNotEmpty(payWay)) {
            if (payWay == 6 && ObjUtils.isEmpty(orderState)) {
                params.put("orderState", null);
            }
        }

        params.put("userPhone", userPhone);
        params.put("orderState", orderState);

        params.put("isAdd", isAdd);
        params.put("bookingType", bookingType);
        if (ObjUtils.isNotEmpty(startDate)) {
            params.put("startDate", startDate + " 00:00:00");
        }
        if (ObjUtils.isNotEmpty(endDate)) {
            params.put("endDate", endDate + " 23:59:59");
        }
        params.put("page", page);
        page = page == null ? 1 : page;
        limit = limit == null ? Integer.MAX_VALUE : limit;
        Page pages = Page.newBuilder(page, limit);

        Page patientPage = sysOrderService.selectOrderList(pages, params);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", patientPage.getItems());
        map.put("records", patientPage.getTotalRecord());
        map.put("total", patientPage.getTotalPage());
        map.put("page", patientPage.getPageNo());
        data.setResults(map);

        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 查询订单列表
     */
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @RequestMapping(value = "/selectOrderSalePageList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "patientId", dataType = "int", required = false, value = "患者id", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "doctorId", dataType = "int", required = false, value = "大夫id", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = false, value = "手机号", defaultValue = "138"),
            @ApiImplicitParam(paramType = "query", name = "startDate", dataType = "string", required = false, value = "开始时间", defaultValue = "2018-03-01"),
            @ApiImplicitParam(paramType = "query", name = "endDate", dataType = "string", required = false, value = "结束时间", defaultValue = "2018-03-05"),
            @ApiImplicitParam(paramType = "query", name = "payWay", dataType = "int", required = false, value = "付款方式:0:余额;1:支付宝二维码;2:微信二维码;3:现金;4:信用卡;5:次卡;6特殊：代表商品订单", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "payWays", dataType = "int", required = false, value = "付款方式:0:余额;1:支付宝二维码;2:微信二维码;3:现金;4:信用卡;5:次卡", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = false, value = "门店id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderState", dataType = "int", required = false, value = "状态：0完成，4取消，null为全部", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "dayType", dataType = "int", required = false, value = "0：全部，1：今天，2：近三天，3：近七天", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "isAdd", dataType = "int", required = false, value = "0继续治疗，1开启新病程", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "bookingType", dataType = "int", required = false, value = "0调理身体，1保健", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectOrderSalePageList(HttpServletRequest request, Integer patientId, Integer doctorId, Integer payWay, Integer payWays, Integer dayType,
                                                String userPhone, Integer subclinicId, Integer orderState, String startDate, String endDate,
                                                Integer page, Integer limit, Integer isAdd, Integer bookingType, Integer userId) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        //Map<String,Object> params = HttpUtils.getAllReqParams(request);
        Map<String, Object> params = new HashMap<>();
        params.put("patientId", patientId);
        params.put("doctorId", doctorId);
        params.put("userId", userId);
        params.put("payWay", payWay);
        params.put("payWays", payWays);
        params.put("dayType", dayType);

        if (ObjUtils.isNotEmpty(subclinicId)) {
            if (subclinicId != 0) {
                params.put("subclinicId", subclinicId);
            }
        }
        if (ObjUtils.isNotEmpty(payWay)) {
            if (payWay == 6 && ObjUtils.isEmpty(orderState)) {
                params.put("orderState", null);
            }
        }

        params.put("userPhone", userPhone);
        params.put("orderState", orderState);

        params.put("isAdd", isAdd);
        params.put("bookingType", bookingType);
        if (ObjUtils.isNotEmpty(startDate)) {
            params.put("startDate", startDate + " 00:00:00");
        }
        if (ObjUtils.isNotEmpty(endDate)) {
            params.put("endDate", endDate + " 23:59:59");
        }
        params.put("page", page);
        page = page == null ? 1 : page;
        limit = limit == null ? Integer.MAX_VALUE : limit;
        Page pages = Page.newBuilder(page, limit);

        Page patientPage = sysOrderService.selectOrderSaleList(pages, params);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", patientPage.getItems());
        map.put("records", patientPage.getTotalRecord());
        map.put("total", patientPage.getTotalPage());
        map.put("page", patientPage.getPageNo());
        data.setResults(map);

        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 商品消耗统计
     */
    @ApiOperation(value = "商品消耗统计", notes = "商品消耗统计")
    @RequestMapping(value = "/selectGoodsStatic", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "typeId", dataType = "string", required = false, value = "日汇总，月汇总：0日汇总；1月汇总", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = false, value = "门店id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectGoodsStatic(String typeId, Integer subclinicId) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        Map<String, Object> params = new HashMap<>();
        params.put("subclinicId", subclinicId);
        params.put("typeId", typeId);
        data.setResults(commonService.selectGoodsStatic(params));
        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 根据推拿服务id订单总价查询次卡和可用优惠券列表
     */
    @ApiOperation(value = "根据推拿服务id订单总价查询次卡和可用优惠券列表", notes = "根据推拿服务id订单总价查询次卡和可用优惠券列表")
    @RequestMapping(value = "/selectUserCardList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cardId", dataType = "int", required = false, value = "1", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderCost", dataType = "int", required = false, value = "20", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectUserCardList(Integer cardId, Integer userId, Integer orderCost) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        Map<String, Object> params = new HashMap<>();
        params.put("cardId", cardId);
        params.put("userId", userId);
        params.put("orderCost", orderCost);

        data.setResults(sysOrderService.selectUserCardList(params));
        // 返回数据
        return data;
    }


    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 提交订单
     */
    @ApiOperation(value = "提交订单", notes = "提交订单")
    @RequestMapping(value = "/submitOrderInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", required = false, value = "订单id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "cashierId", dataType = "int", required = false, value = "后台登录用户id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "payWay", dataType = "int", required = false, value = "推拿支付方式：支付方式:0:余额;1:支付宝二维码;2:微信二维码;3:现金;4:信用卡;5:次卡", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "payWays", dataType = "int", required = false, value = "商品支付方式：支付方式:0:余额;1:支付宝二维码;2:微信二维码;3:现金;4:信用卡;5:次卡", defaultValue = "1"),

    })
    @ResponseBody
    public ResponseData submitOrderInfo(HttpServletRequest request,
                                        Integer cashierId, Integer orderId, String userName, Integer payWay,
                                        Integer payWays, String check, String userPhone) {

        //SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);

        // 创建响应Model
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderId, userName});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        // 调用Service
        Map<String, Object> params = new HashMap<>();
        params.put("userName", userName);
        params.put("cashierId", cashierId);
        params.put("payWay", payWay);//推拿支付方式
        params.put("payWays", payWays);//商品支付方式
        params.put("orderId", orderId);
        params.put("check", check);
        params.put("userPhone", userPhone);
        // 返回数据

        try {
            data = sysOrderService.saveOrderInfo(params);
        } catch (Exception e) {
            data.setCodeEnum(CodeEnum.CODE_30002);
            e.printStackTrace();

        }

        return data;
    }


    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 代付订单-发送验证码
     */
    @ApiOperation(value = "代付订单-发送验证码", notes = "代付订单-发送验证码")
    @RequestMapping(value = "/replacePayOrder", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = false, value = "代付人手机号", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", required = false, value = "订单id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData replacePayOrder(Integer orderId, String userPhone) {

        // 创建响应Model
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderId, userPhone});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        // 调用Service
        Map<String, Object> params = new HashMap<>();
        params.put("userPhone", userPhone);
        params.put("orderId", orderId);
        // 返回数据
        return sysOrderService.updateOrderInfo(params, data);
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 代付订单-校验验证码|余额
     */
    @ApiOperation(value = "代付订单-校验验证码|余额", notes = "代付订单-校验验证码|余额")
    @RequestMapping(value = "/savePayOrder", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", required = false, value = "订单id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = false, value = "手机号", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "check", dataType = "string", required = false, value = "验证码", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData savePayOrder(Integer orderId, String userPhone, String check) {

        // 创建响应Model
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderId, check});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        // 返回数据

        return sysOrderService.updatePayOrder(orderId, userPhone, check, data);
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 取消代付
     */
    @ApiOperation(value = "取消代付", notes = "取消代付")
    @RequestMapping(value = "/updateOtherPayOrderInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", required = false, value = "订单id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData updateOtherPayOrderInfo(Integer orderId) {

        // 创建响应Model
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderId});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        // 返回数据

        return sysOrderService.updateOtherPayOrderInfo(orderId, data);
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 修改已支付订单的推拿大夫
     */
    @ApiOperation(value = "取消代付", notes = "取消代付")
    @RequestMapping(value = "/updateOrderDoctor", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", value = "订单id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "doctorId", dataType = "int", value = "修改大夫id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "doctorSubId", dataType = "int", value = "修改大夫的分店id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData updateOrderDoctor(Integer orderId, Integer doctorId, Integer doctorSubId) {

        // 创建响应Model
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderId});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        Integer res = sysOrderService.updateOrderDoctor(orderId, doctorId, doctorSubId);
        if (res == -1) {
            data.setCodeEnum(CodeEnum.CODE_30007);
        } else if (res == -2) {
            data.setCodeEnum(CodeEnum.CODE_30008);
        } else if (res == -3) {
            data.setCodeEnum(CodeEnum.CODE_30009);
        }

        // 返回数据
        data.setResults(res);
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: 修改已支付订单的推拿大夫
     */
    @ApiOperation(value = "取消代付", notes = "取消代付")
    @RequestMapping(value = "/updateAccountUserInfoDoctor", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", dataType = "int", value = "订单id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "doctorId", dataType = "int", value = "修改大夫id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "doctorSubId", dataType = "int", value = "修改大夫的分店id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData updateAccountUserInfoDoctor(Integer orderId, Integer doctorId, Integer doctorSubId) {

        // 创建响应Model
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderId});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        Integer res = sysOrderService.updateAccountUserInfoDoctor(orderId, doctorId, doctorSubId);
        if (res == -1) {
            data.setCodeEnum(CodeEnum.CODE_30007);
        } else if (res == -2) {
            data.setCodeEnum(CodeEnum.CODE_30001);
        } else if (res == -3) {
            data.setCodeEnum(CodeEnum.CODE_30009);
        }

        // 返回数据
        data.setResults(res);
        return data;
    }


    /**
     * @throws
     * @Description: 新增加修改订单支付方式
     */
    @ApiOperation(value = "新增加修改订单支付方式", notes = "新增加修改订单支付方式")
    @RequestMapping(value = "/updateOrderInfoByPayFlag", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData updateOrderInfoByPayFlag(Integer payWay, Integer payWays, Integer orderId) {
        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        Integer result = doctorService.updateOrderInfoByPayFlag(payWay, payWays, orderId);
        if (result == -1) {
            data.setCodeEnum(CodeEnum.CODE_30010);
        } else if (result == -2) {
            data.setCodeEnum(CodeEnum.CODE_30011);
        } else if (result == -3) {
            data.setCodeEnum(CodeEnum.CODE_30012);
        } else if (result == -4) {
            data.setCodeEnum(CodeEnum.CODE_60008);
        }
        data.setResults(result);
        // 返回数据
        return data;
    }

    /**
     * @throws
     * @Description: 新增加修改卡项订单支付方式
     */
    @ApiOperation(value = "新增加修改卡项订单支付方式", notes = "新增加修改卡项订单支付方式")
    @RequestMapping(value = "/updateAccountUserInfoByPayFlag", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData updateAccountUserInfoByPayFlag(Integer payWay, Integer orderId) {
        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        Integer result = doctorService.updateAccountUserInfoByPayFlag(payWay, orderId);
        if (result == -1) {
            data.setCodeEnum(CodeEnum.CODE_30010);
        } else if (result == -2) {
            data.setCodeEnum(CodeEnum.CODE_30011);
        } else if (result == -3) {
            data.setCodeEnum(CodeEnum.CODE_30015);
        } else if (result == -4) {
            data.setCodeEnum(CodeEnum.CODE_60008);
        }
        data.setResults(result);
        // 返回数据
        return data;
    }

}
